package com.mengxuegu.controller;

import com.mengxuegu.springcloud.entities.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author william
 * @create 2021-06-21 22:24
 **/
@RestController
public class ProductController_Consumer {
//    private static final String REST_URL_PREFIX = "http://localhost:8001";
    //修改为商品提供者向Eureka服务器中注册的地址
    private static final String REST_URL_PREFIX = "http://microservice-product";
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/consumer/product/add", method = RequestMethod.POST)
    public boolean add(@RequestBody Product product) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/product/add", product, Boolean.class);
    }


    @RequestMapping(value = "/consumer/product/get/{id}", method = RequestMethod.GET)
    public Product get(@PathVariable Long id) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/product/get/" + id, Product.class);
    }

    @RequestMapping(value = "/consumer/product/list", method = RequestMethod.GET)
    public List<Product> list() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/product/list", List.class);
    }


}
